package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.SkuProperty;
import com.xmg.shop.api.service.ISkuPropertyService;
import com.xmg.shop.api.util.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/skuProperty")
public class SkuPropertyController {
    @Reference
    private ISkuPropertyService skuPropertyService;

    @RequestMapping()
    public String skuPropertyPage() {
        return "sku/property";
    }

    @RequestMapping("/get/{catalogId}")
    public String get(@PathVariable("catalogId") Long catalogId, Model model) {
        model.addAttribute("list", skuPropertyService.selectByCatalogId(catalogId));
        return "sku/property_list";
    }

    @RequestMapping("/save")
    public String save(SkuProperty skuProperty, Model model) {
        if (skuProperty.getId() > -1) {
            model.addAttribute("skuProperty", skuPropertyService.selectByskuPropertyId(skuProperty.getId()));
        }
        return "sku/property_save";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long skuPropertyId) {
        AjaxResult result = null;
        try {
            skuPropertyService.delete(skuPropertyId);
            result = new AjaxResult("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult add(SkuProperty skuProperty) {
        AjaxResult result = null;
        try {
            if (skuProperty.getId() == null || skuProperty.getId() == -1) {
                skuPropertyService.save(skuProperty);
                result = new AjaxResult("保存成功");
            } else {
                skuPropertyService.update(skuProperty);
                result = new AjaxResult("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        }
        return result;
    }

}
